package edu.uow.devdocs

import edu.uow.devdocs.Person

class AssignmentService {
	def emailService

    def Assignment createDefaultAssignment(DevelopmentTask task, Project project, Person person) {
		Assignment assignment = createDefaultAssignment()
		assignment.person = person
		assignment.developmentTask = task
		assignment.save()
		emailService.notifyNewAssignment(assignment)
		return assignment
    }
	
	def Assignment createDefaultAssignment() {
		def assignment = new Assignment()
		assignment.status = AssignmentStatus.getOpenStatus()
		assignment.save(flush:true)
		return assignment
	}
	
	def addToProject(int personId, int projectId, Assignment assignment) {
		if (Person.findByPersonAndProject(personId,projectId).list().size() == 0) {
			//Assigned person does not have access to the Project
			def person = assignment.person
			person.addToProjects(assignment.project)
			person.save()
		}
	}
}
